import pygame
import sys
import random

# 初始化Pygame
pygame.init()

# 设置窗口大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))

# 设置标题
pygame.display.set_caption("扫雷游戏")

# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)

# 游戏参数
grid_size = 20  # 每个格子的大小
rows = 16  # 行数
cols = 30  # 列数
mines_count = 99  # 地雷数量

# 生成游戏网格
grid = [[{'mine': False, 'revealed': False, 'flagged': False, 'neighbors': 0} for _ in range(cols)] for _ in range(rows)]

# 在网格中随机放置地雷
mines = set()
while len(mines) < mines_count:
    x = random.randint(0, cols - 1)
    y = random.randint(0, rows - 1)
    if (x, y) not in mines:
        grid[y][x]['mine'] = True
        mines.add((x, y))

# 计算每个格子周围的地雷数量
for y in range(rows):
    for x in range(cols):
        if not grid[y][x]['mine']:
            for dx in range(-1, 2):
                for dy in range(-1, 2):
                    nx, ny = x + dx, y + dy
                    if 0 <= nx < cols and 0 <= ny < rows and grid[ny][nx]['mine']:
                        grid[y][x]['neighbors'] += 1

# 绘制游戏界面
def draw_game_interface():
    screen.fill(white)
    for y in range(rows):
        for x in range(cols):
            cell = grid[y][x]
            if cell['revealed']:
                if cell['mine']:
                    pygame.draw.circle(screen, red, (x * grid_size + grid_size // 2, y * grid_size + grid_size // 2), grid_size // 3)
                else:
                    color = (128, 128, 128) if cell['neighbors'] > 0 else white
                    text = str(cell['neighbors']) if cell['neighbors'] > 0 else ''
                    text_surf = font.render(text, True, black)
                    text_rect = text_surf.get_rect(center=(x * grid_size + grid_size // 2, y * grid_size + grid_size // 2))
                    pygame.draw.rect(screen, color, (x * grid_size, y * grid_size, grid_size, grid_size))
                    screen.blit(text_surf, text_rect)
            elif cell['flagged']:
                pygame.draw.rect(screen, green, (x * grid_size, y * grid_size, grid_size, grid_size))
                pygame.draw.circle(screen, black, (x * grid_size + grid_size // 2, y * grid_size + grid_size // 2), grid_size // 3)
            else:
                pygame.draw.rect(screen, black, (x * grid_size, y * grid_size, grid_size, grid_size), 1)

# 主循环
font = pygame.font.Font(None, 36)
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            x, y = pygame.mouse.get_pos()
            col = x // grid_size
            row = y // grid_size
            if event.button == 1:  # 左键
                grid[row][col]['revealed'] = True
            elif event.button == 3:  # 右键
                grid[row][col]['flagged'] = not grid[row][col]['flagged']

    draw_game_interface()
    pygame.display.flip()

# 清理并退出Pygame
pygame.quit()
sys.exit()